
#include <BFile>
#include <BXmlDocument>

#include "StringUtils.h"
#include "Config.h"

Config::Config()
{
	_dirty = false;
	_targetSpaceVisual = true;
	_objectSpaceVisual = true;
	_shapeSpaceVisual = true;
	_targetBoxVisual = true;
	_objectBoxVisual = true;
	_shapeBoxVisual = true;
	_selectionInspecting = true;

	_showTabs = false;
	_showSpaces = false;
	_showReturns = false;
	_showLinefeeds = false;
}
Config::~Config()
{

}

bool Config::load()
{
	BString fileName = this->name();
	BFile file(fileName);
	if (!file.open(IO_Read))
	{
		return false;
	}

	BString errorMsg;
	BXmlDocument xmldoc;
	BXmlNode* root = xmldoc.read(file);
	if (!root)
	{
		return false;
	}
	if (BXmlNode* node = root->child("grid-line"))
	{
		if (BXmlNode* visualNode = node->child("visual"))
			_gridLineVisual = visualNode->text() == "true";
		if (BXmlNode* sizeNode = node->child("size"))
			_gridLineSize = sizeNode->text().toReal();
		if (BXmlNode* segmentsNode = node->child("segments"))
			_gridLineSegments = segmentsNode->text().toInt();
		if (BXmlNode* colorNode = node->child("color"))
			_gridLineColor = StringToColor(colorNode->text());
	}

	if (BXmlNode* node = root->child("target-space"))
	{
		_targetSpaceVisual = node->attrib("visual") == "true";
		_targetSpaceColor = StringToColor(node->attrib("color"));
	}
	if (BXmlNode* node = root->child("object-space"))
	{
		_objectSpaceVisual = node->attrib("visual") == "true";
		_objectSpaceColor = StringToColor(node->attrib("color"));
	}
	if (BXmlNode* node = root->child("shape-space"))
	{
		_shapeSpaceVisual = node->attrib("visual") == "true";
		_shapeSpaceColor = StringToColor(node->attrib("color"));
	}

	if (BXmlNode* node = root->child("target-box"))
	{
		_targetBoxVisual = node->attrib("visual") == "true";
		_targetBoxColor = StringToColor(node->attrib("color"));
	}
	if (BXmlNode* node = root->child("object-box"))
	{
		_objectBoxVisual = node->attrib("visual") == "true";
		_objectBoxColor = StringToColor(node->attrib("color"));
	}
	if (BXmlNode* node = root->child("shape-box"))
	{
		_shapeBoxVisual = node->attrib("visual") == "true";
		_shapeBoxColor = StringToColor(node->attrib("color"));
	}

	if (BXmlNode* node = root->child("selection-inspecting"))
	{
		_selectionInspecting = node->text() == "true";
	}
	if (BXmlNode* node = root->child("show-tabs"))
	{
		_showTabs = node->text() == "true";
	}
	if (BXmlNode* node = root->child("show-spaces"))
	{
		_showSpaces = node->text() == "true";
	}
	if (BXmlNode* node = root->child("show-returns"))
	{
		_showReturns = node->text() == "true";
	}
	if (BXmlNode* node = root->child("show-linefeed"))
	{
		_showLinefeeds = node->text() == "true";
	}
	_dirty = false;
	return true;
}
bool Config::save()
{
	BString fileName = this->name();
	BFile file(fileName);
	if (!file.open(IO_Write))
	{
		return false;
	}

	BXmlDocument xmldoc;
	xmldoc.setFormat("/>\n");
	BXmlNode* root = new BXmlNode("config");
	root->setFormat(">\n</>\n");
	if (BXmlNode* node = root->addChild("grid-line"))
	{
		node->setFormat(">\n/>\n");
		if (BXmlNode* visualNode = node->addChild("visual"))
		{
			visualNode->setFormat("");
			visualNode->setText(_gridLineVisual ? "true" : "false");
		}
		if (BXmlNode* sizeNode = node->addChild("size"))
			sizeNode->setText(BString(_gridLineSize));
		if (BXmlNode* segmentsNode = node->addChild("segments"))
			segmentsNode->setText(BString(_gridLineSegments));
		if (BXmlNode* colorNode = node->addChild("color"))
			colorNode->setText(StringFromColor(_gridLineColor));
	}
	if (BXmlNode* node = root->addChild("target-space"))
	{
		node->setAttrib("visual", _targetSpaceVisual ? "true" : "false");
		node->setAttrib("color", StringFromColor(_targetSpaceColor));
	}
	if (BXmlNode* node = root->addChild("object-space"))
	{
		node->setAttrib("visual", _objectSpaceVisual ? "true" : "false");
		node->setAttrib("color", StringFromColor(_objectSpaceColor));
	}
	if (BXmlNode* node = root->addChild("shape-space"))
	{
		node->setAttrib("visual", _shapeSpaceVisual ? "true" : "false");
		node->setAttrib("color", StringFromColor(_shapeSpaceColor));
	}
	if (BXmlNode* node = root->addChild("target-box"))
	{
		node->setAttrib("visual", _targetBoxVisual ? "true" : "false");
		node->setAttrib("color", StringFromColor(_targetBoxColor));
	}
	if (BXmlNode* node = root->addChild("object-box"))
	{
		node->setAttrib("visual", _objectBoxVisual ? "true" : "false");
		node->setAttrib("color", StringFromColor(_objectBoxColor));
	}
	if (BXmlNode* node = root->addChild("shape-box"))
	{
		node->setAttrib("visual", _shapeBoxVisual ? "true" : "false");
		node->setAttrib("color", StringFromColor(_shapeBoxColor));
	}
	if (BXmlNode* node = root->addChild("selection-inspecting"))
	{
		node->setText(_selectionInspecting ? "true" : "false");
	}
	if (BXmlNode* node = root->addChild("show-tabs"))
	{
		node->setText(_showTabs ? "true" : "false");
	}
	if (BXmlNode* node = root->addChild("show-spaces"))
	{
		node->setText(_showSpaces ? "true" : "false");
	}
	if (BXmlNode* node = root->addChild("show-returns"))
	{
		node->setText(_showReturns ? "true" : "false");
	}
	if (BXmlNode* node = root->addChild("show-linefeed"))
	{
		node->setText(_showLinefeeds ? "true" : "false");
	}

	bool res = xmldoc.write(file, root);
	_dirty = false;
	return true;
}
bool Config::isDirty() const
{
	return _dirty;
}

void Config::setGridLineVisual(bool gridLineVisual)
{
	if (_gridLineVisual != gridLineVisual)
	{
		_gridLineVisual = gridLineVisual;
		_dirty = true;
	}
}
bool Config::gridLineVisual() const
{
	return _gridLineVisual;
}

void Config::setGridLineSize(BReal gridLineSize)
{
	if (_gridLineSize != gridLineSize)
	{
		_gridLineSize = gridLineSize;
		_dirty = true;
	}
}
BReal Config::gridLineSize() const
{
	return _gridLineSize;
}

void Config::setGridLineSegments(int gridLineSegments)
{
	if (_gridLineSegments != gridLineSegments)
	{
		_gridLineSegments = gridLineSegments;
		_dirty = true;
	}
}
int Config::gridLineSegments() const
{
	return _gridLineSegments;
}
void Config::setGridLineColor(const BColor& gridLineColor)
{
	if (_gridLineColor != gridLineColor)
	{
		_gridLineColor = gridLineColor;
		_dirty = true;
	}
}
const BColor& Config::gridLineColor() const
{
	return _gridLineColor;
}

void Config::setTargetSpaceVisual(bool targetSpaceVisual)
{
	if (_targetSpaceVisual != targetSpaceVisual)
	{
		_targetSpaceVisual = targetSpaceVisual;
		_dirty = true;
	}
}
bool Config::targetSpaceVisual() const
{
	return _targetSpaceVisual;
}
void Config::setTargetSpaceColor(const BColor& color)
{
	if (_targetSpaceColor != color)
	{
		_targetSpaceColor = color;
		_dirty = true;
	}
}
const BColor& Config::targetSpaceColor() const
{
	return _targetSpaceColor;
}

void Config::setObjectSpaceVisual(bool objectSpaceVisual)
{
	if (_objectSpaceVisual != objectSpaceVisual)
	{
		_objectSpaceVisual = objectSpaceVisual;
		_dirty = true;
	}
}
bool Config::objectSpaceVisual() const
{
	return _objectSpaceVisual;
}
void Config::setObjectSpaceColor(const BColor& color)
{
	if (_objectSpaceColor != color)
	{
		_objectSpaceColor = color;
		_dirty = true;
	}
}
const BColor& Config::objectSpaceColor() const
{
	return _objectSpaceColor;
}

void Config::setShapeSpaceVisual(bool shapeSpaceVisual)
{
	if (_shapeSpaceVisual != shapeSpaceVisual)
	{
		_shapeSpaceVisual = shapeSpaceVisual;
		_dirty = true;
	}
}
bool Config::shapeSpaceVisual() const
{
	return _shapeSpaceVisual;
}
void Config::setShapeSpaceColor(const BColor& shapeSpaceColor)
{
	if (_shapeSpaceColor != shapeSpaceColor)
	{
		_shapeSpaceColor = shapeSpaceColor;
		_dirty = true;
	}
}
const BColor& Config::shapeSpaceColor() const
{
	return _shapeSpaceColor;
}

void Config::setTargetBoxVisual(bool targetBoxVisual)
{
	if (_targetBoxVisual != targetBoxVisual)
	{
		_targetBoxVisual = targetBoxVisual;
		_dirty = true;
	}
}
bool Config::targetBoxVisual() const
{
	return _targetBoxVisual;
}
void Config::setTargetBoxColor(const BColor& targetBoxColor)
{
	if (_targetBoxColor != targetBoxColor)
	{
		_targetBoxColor = targetBoxColor;
		_dirty = true;
	}
}
const BColor& Config::targetBoxColor() const
{
	return _targetBoxColor;
}

void Config::setObjectBoxVisual(bool objectBoxVisual)
{
	if (_objectBoxVisual != objectBoxVisual)
	{
		_objectBoxVisual = objectBoxVisual;
		_dirty = true;
	}
}
bool Config::objectBoxVisual() const
{
	return _objectBoxVisual;
}
void Config::setObjectBoxColor(const BColor& objectBoxColor)
{
	if (_objectBoxColor != objectBoxColor)
	{
		_objectBoxColor = objectBoxColor;
		_dirty = true;
	}
}
const BColor& Config::objectBoxColor() const
{
	return _objectBoxColor;
}

void Config::setShapeBoxVisual(bool shapeBoxVisual)
{
	if (_shapeBoxVisual != shapeBoxVisual)
	{
		_shapeBoxVisual = shapeBoxVisual;
		_dirty = true;
	}
}
bool Config::shapeBoxVisual() const
{
	return _shapeBoxVisual;
}
void Config::setShapeBoxColor(const BColor& color)
{
	if (_shapeBoxColor != color)
	{
		_shapeBoxColor = color;
		_dirty = true;
	}
}
const BColor& Config::shapeBoxColor() const
{
	return _shapeBoxColor;
}

void Config::setSelectionInspecting(bool selectionInspecting)
{
	if (_selectionInspecting != selectionInspecting)
	{
		_selectionInspecting = selectionInspecting;
		_dirty = true;
	}
}
bool Config::selectionInspecting() const
{
	return _selectionInspecting;
}

void Config::setShowTabs(bool show)
{
	if (_showTabs != show)
	{
		_showTabs = show;
		_dirty = true;
	}
}
bool Config::showTabs() const
{
	return _showTabs;
}

void Config::setShowSpaces(bool show)
{
	if (_showSpaces != show)
	{
		_showSpaces = show;
		_dirty = true;
	}
}
bool Config::showSpaces() const
{
	return _showSpaces;
}

void Config::setShowReturns(bool show)
{
	if (_showReturns != show)
	{
		_showReturns = show;
		_dirty = true;
	}
}
bool Config::showReturns() const
{
	return _showReturns;
}

void Config::setShowLinefeeds(bool show)
{
	if (_showLinefeeds != show)
	{
		_showLinefeeds = show;
		_dirty = true;
	}
}
bool Config::showLinefeeds() const
{
	return _showLinefeeds;
}
